package com.yb0os1.serve.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yb0os1.constant.MessageConstant;
import com.yb0os1.constant.StatusConstant;
import com.yb0os1.dto.DishDTO;
import com.yb0os1.entity.Dish;
import com.yb0os1.entity.DishFlavors;
import com.yb0os1.exception.DeletionNotAllowedException;
import com.yb0os1.mapper.CategoryMapper;
import com.yb0os1.mapper.DishFlavorMapper;
import com.yb0os1.mapper.DishMapper;
import com.yb0os1.mapper.SetmealDishMapper;
import com.yb0os1.result.PageResult;
import com.yb0os1.result.Result;
import com.yb0os1.serve.DishServe;
import com.yb0os1.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class DishServeImpl implements DishServe {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }
    @Override
    public List<Dish> getDishByTypeId(Long categoryId) {
        return dishMapper.selectByTypeId(categoryId);
    }

    @Override
    public void updateDish(DishDTO dishDTO){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        List<DishFlavors> flavors = dishDTO.getFlavors();
        //先删后加 即为更新
        dishFlavorMapper.deleteByDishId(dish.getId());
        if (flavors!=null && !flavors.isEmpty()){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public DishVO getDishById(Long id) {
        DishVO dishVO = dishMapper.selectById(id);
        dishVO.setFlavors(dishFlavorMapper.selectByDishId(id));
        return dishVO;
    }

    @Override
    public void deleteDish(List<Long> ids) {

        //正在出出售的菜品不可删除
        for (Long id : ids) {
            DishVO dish =  dishMapper.selectById(id);
            if (dish.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //当前菜品是否和套餐关联
        // 这里的判断主要是为了以后的套餐管理的模块
        List<Long> setmealIDs = setmealDishMapper.getSetmealIDsByDishIds(ids);
        if (setmealIDs!=null && !setmealIDs.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 这样就可以删除了 dish和dish_flavor一块删除
        dishMapper.deleteDishAndFlavor(ids);
    }


    @Override
    @Transactional
    public void addDishAndFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insertDish(dish);
        //本身是获取不了的 但是在mapperxml中设置了将生成的主键赋值为id属性
        Long id = dish.getId();
        List<DishFlavors> flavors = dishDTO.getFlavors();
        if (flavors!=null && !flavors.isEmpty()){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public PageResult pageSelect(Long categoryId, String name, Integer page, Integer pageSize, Integer status) {
        PageHelper.startPage(page,pageSize);
        List<DishVO> dishList = dishMapper.select(categoryId,name,status);
        Page<DishVO> p = (Page<DishVO>) dishList;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList =  dishMapper.list(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            String categoryName = categoryMapper.getCategoryName(d.getCategoryId());
            List<DishFlavors> flavors = dishFlavorMapper.selectByDishId(d.getId());
            dishVO.setCategoryName(categoryName);
            log.info("PPPPP{}",flavors);
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        log.info("dishVOList{}",dishVOList);
        return dishVOList;
    }
}
